<?php
class MY_Controller extends CI_Controller
{
    protected $errorMessage;
    protected $successMessage = '成功';
    protected $errorCode = 200;
    public $queryData = [];
    public $userId = 0;
    public $userDetail = [];
    public $isArrays = false;
    public function __construct()
    {
        parent::__construct();
        
        define("SITEURL","http://59.110.68.88:9898/");
        define("BASEURL","http://59.110.68.88:9898/");
        define("STATICURL",BASEURL."static/");
        define("UPLOADURL",BASEURL."upload/");
        define("VIEWURL",BASEURL."views/");
        define("OSSHOST",$this->config->item("oss")['host']);
        define("FRONT_STATICURL",BASEURL.'public/statics/');
        $auth_list = $this->config->item('AUTH_LIST');
        $this->queryData = $this->getFileContent();
        $token      = isset($this->queryData['token']) ? $this->queryData['token'] : '';
        if(in_array(strtolower($this->uri->uri_string()), $auth_list) || $token)
        {
            $this->checkLogin($token);
        }
    }

     /**
     * 生成接口
     *
     * @param unknown_type $status 状态值
     * @param unknown_type $result
     * @param unknown_type $des
     */
    public function createApi($result ="",$extend = array()){
        $errorCode = $this->config->item('errorCode');
        if($this->errorCode == 200) {
            $api['code'] = $this->errorCode;
            $api['message'] = $errorCode[$this->errorCode];
            if($this->isArrays == true){
                $api['data'] = empty($result) ? [] : $result;
            }else{
                $api['data'] = empty($result) ? new stdClass() : $result;
            }
            
        }else{
            $api['code'] = $this->errorCode;
            $api['message'] = $this->errorMessage?$this->errorMessage:$errorCode[$this->errorCode];
            if($this->isArrays == true){
                $api['data'] = empty($result) ? [] : $result;
            }else{
                $api['data'] = empty($result) ? new stdClass() : $result;
            }
        }
        header("Content-type: application/json; charset=utf-8");
        echo json_encode($api);
    }

    public function getFileContent(){
        $json_content = urldecode(file_get_contents('php://input')) ? urldecode(file_get_contents('php://input')) : 'clientOs=1';
        file_put_contents("mylog.txt", "--".$json_content."\r\n",FILE_APPEND);
    $json_content_arr = $this->convertUrlQuery($json_content); 
        $clientOs   = isset($json_content_arr['clientOs']) ? $json_content_arr['clientOs'] : '';
        $validate = new Validate(['clientOs' => 'require|number']);
        if (!$validate->check(['clientOs'=>$clientOs])) 
        {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        return $json_content_arr; 
    } 
    function convertUrlQuery($query) {
        $queryParts = explode('&', $query);
        $params = array();
        foreach ($queryParts as $param)
        {
            $item = explode('=', $param);
            $params[$item[0]] = $item[1];
        }
        return $params;
    }


    public function checkLogin($token)
    {
        $validate = new Validate(['token' => 'require|string']);
        if (!$validate->check(['token'=>$token])) 
        {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $this->load->model('user_model', 'user');
        $res = $this->user->checkToken($token);
        if(empty($res))
        {
            $this->errorCode = 50009;
            $this->createApi();
            exit;
        }
        $this->userDetail = $this->userInfo($token);
    }
    /**
     * 登陆状态检测
     */
    
    public function userInfo($token){
        $this->load->model("user_model","user");
        $userInfo = $this->user->getUserInfoByToken($token);
        if($userInfo['avatar'])
        {
            $userInfo['avatar'] = imgFormat($userInfo['avatar']);
        }

        if($userInfo['AccountID']){
            $this->user->edit_user($userInfo['AccountID'],array("last_use_time"=>time()));    
        }
        return $userInfo;
    }
    public function setPoint($id)
    {
        $this->load->model('point_model', 'point');
        $point_row = $this->point->get_point_by_id($id);
        return $point_row['points'];
    }
}


class Auth_Controller extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $blackList = $this->config->item('NO_AUTH_LIST');
        $this->queryData = $this->getFileContent();
        $token      = isset($this->queryData['token']) ? $this->queryData['token'] : '';
        if(!in_array(strtolower($this->uri->uri_string()), $blackList))
        {
            $this->checkLogin($token);
        }
    }
}

   

//后台管理控制器
class MY_Luckydog extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('encrypt');
        define("BASEURL","http://59.110.68.88:9898/");
        define("STATICURL",BASEURL."Public/luckydogstatics/");
        define("UPLOADURL",BASEURL.'uploads');
        define("OSSHOST",$this->config->item("oss")['host']);
        define("FRONT_STATICURL",BASEURL.'public/statics/');
        $menuList = $this->config->item("menu");
        $this->load->vars(["menuList"=>$menuList]);
        $this->init();

    }

    public function init()
    {
        $currentClass = $this->router->fetch_class(); 
        if($currentClass != "login"){
            $login_status  = $this->check_login_status();
            if (empty($login_status)){
                redirect('/luckydog/login');die;
            }
        }
    }

	 public function setPoint($id)
		{
			$this->load->model('point_model', 'point');
			$point_row = $this->point->get_point_by_id($id);
			return $point_row['points'];
		}
    /**
     * 登陆状态检测
     */
    public function check_login_status()
    {
        $this->load->model('luckydog/admin_user_model', 'admin_user');
        return $this->admin_user->get_login_status();
    }



    function create_page_links($base_url,$total_cnt,$limit)
    {
        $this->load->library('Pagination');
        $conf_page  = array('base_url'=>$base_url,
            'total_rows'=>$total_cnt,
            'per_page'=>$limit,
            'page_query_string'=>true,
            'first_tag_open'=>'<span class="page_btn">',
            'first_tag_close'=>'</span>',
            'last_tag_open'=>'<span  class="page_btn">',
            'last_tag_close'=>'</span>',
            'prev_tag_open'=>'<span  class="page_btn">',
            'prev_tag_close'=>'</span>',
            'next_tag_open'=>'<span  class="page_btn">',
            'next_tag_close'=>'</span>',
            'num_tag_open'=>'<span  class="page_btn">',
            'num_tag_close'=>'</span>',
            'cur_tag_open'=>'<span  class="page_btn current">',
            'cur_tag_close'=>'</span>',
            'first_link'=>'首页',
            'prev_link'=>'上页',
            'next_link'=>'下页',
            'last_link'=>'末页',
            'num_links'=>5,
            'total_lang'=>'共',
            'pages_lang'=>'页',
            'tiao_lang'=>'条');
        $this->pagination->initialize($conf_page);
        $page_links = $this->pagination->create_links();
        return $page_links;
    }



}

?>
